<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
//实现方法二需要这个门面类
use Validator;
use App\Manager;

//引入Auth门面
use Illuminate\Support\Facades\Auth;

class ManagerController extends Controller
{
    //
	function login(Request $request){
		if($request->isMethod('get')){
			//显示视图
			return view('Admin.login');
		}elseif($request->isMethod('post')){
			//数据处理
			//1、数据验证（用户名长度，是否为空）
			//实现方法一：使用validate方法
			/* $this->validate($request, [
				'username' => 'required|min:2|max:16',
				'password' => 'required|between:4,20',
				'captcha' => 'required|size:5|captcha',
			]); */
			//实现方法二：使用validator门面
			$validator = Validator::make($request->all(), [
				'username' => 'required|min:2|max:16',
				'password' => 'required|between:4,20',
				// 'captcha' => 'required|size:5|captcha',
			]);
			
			if ($validator->fails()) {
				return redirect('/admin/login')
						->withErrors($validator)
						->withInput();
			}
			
			//2、用户认证（用户名与密码在数据库是否能直接查询匹配到）
			$username = $request->input('username');
			$password = $request->input('password');
			if(Auth::guard('admin')->attempt(['username' => $username, 'password' => $password])){
				// echo "认证成功！";
				return redirect('/admin/index');
			}else{
				// echo '认证失败！';
				return redirect('/admin/login')
					->withErrors(['loginError' => "用户名或密码错误"])
					->withInput();
			}
		}
	}
	
	function logout(){
		Auth::guard('admin')->logout();
		return redirect('/admin/login');
	}
	
	function lst(){
		//获取所有的数据
		// $data = Manager::all();
		// $data = Manager::paginate(5); //每页显示5条数据
		// $data = Manager::where(['created_at' => '2021-11-29 03:47:15'])->paginate(5);
		// $data = Manager::where([['created_at', '>=', '2021-11-29 03:47:13'], ['created_at', '<=', '2021-11-29 03:47:15']])->paginate(5);
		// $data = Manager::whereDay('created_at', '28')->paginate(5);
		$data = Manager::whereDate('created_at', '2021-11-28')->paginate(5);
		// $data = Manager::whereYear('created_at', '2021')->whereMonth('created_at', '11')->whereDay('created_at', '28')->paginate(5);
		
		return view('Admin.manager.lst', [
			'data' => $data,
		]);
	}
	
	function add(Request $request){
		if($request->isMethod('get')){
			//显示视图
			return view('Admin.manager.add');
		}elseif($request->isMethod('post')){
			//数据处理
			// dd($request->all());
			$manager = Manager::create($request->all());
			//使用加密函数
			$manager->password = bcrypt($request->input('password'));
			
			$file = $request->file('file');
			if($file->isValid()){
				$path = $file->store('public');
				$manager->mg_pic = str_replace('public', '/storage', $path);
			}
			
			//保存到数据库
			if($manager->save()){
				return ['success' => true];
			}else{
				return ['success' => false];
			}
		}
	}
	
	function del(Request $request){
		// dd($request->all());
		
		$id = $request->input('mg_id');
		$re = Manager::find($id)->delete();
		if($re === false){
			return ['success' => false];
		}else{
			return ['success' => true];
		}
	}
	
	
}
